package com.yingxin.yxerp.module.sys.controller;

import com.yingxin.yxerp.module.sys.entity.Company;
import com.yingxin.yxerp.module.sys.mapper.CompanyMapper;
import com.yingxin.yxerp.module.sys.service.ICompanyService;
import com.yingxin.yxerp.module.sys.vo.req.CompanyQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.CompanyRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分公司 前端控制器
 * @author fanchao
 * @since 2021-05-13
 */

@RestController
public class CompanyController {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private CompanyMapper companyMapper;

    @ApiOperation(value = "分公司列表")
    @PostMapping("/organization/company/list")
    public List<CompanyRespVo> listCompany(@RequestBody(required = false) CompanyQueryVo queryVo) {
        if (queryVo == null) {
            queryVo = new CompanyQueryVo();
        }
        return companyMapper.listVo(queryVo);
    }

    @ApiOperation(value = "主要生产线（酸洗线、轧硬线、镀锌线）且正常开机工作的（未被禁用的）")
    @PostMapping("/company/real/in/production/list")
    public List<CompanyRespVo> realInProductionList(@RequestBody(required = false) CompanyQueryVo queryVo) {
        return companyMapper.realInProductionList( queryVo );
    }

    @PreAuthorize("hasAuthority('company:updateMerchant')")
    @ApiOperation(value = "更新关联客户")
    @PostMapping("/organization/company/updateMerchant")
    public void updateMerchantId(@RequestParam("id") String id, @RequestParam("merchantId") String merchantId) {
        Company company = companyService.getById(id);
        company.setMerchantId(merchantId);
        companyService.updateMerchantId(company);
    }

}